package dao;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import modelo.Clasificacionmenu;
import modelo.Detallemenu;
import modelo.DetallemenuId;
import modelo.Menu;
import modelo.Salon;
import modelo.Tipomenu;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;

public class MenuDao implements Serializable {

	private static final long serialVersionUID = 1633181053454014755L;

	private Session session;
	SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	public MenuDao() {
		super();
	}
	
	public List<Menu> obtenerMenu(){
		session=sessionFactory.openSession();

		Criteria cr = session.createCriteria(Menu.class);
		List<Menu> results = cr.list();
		
		session.close();
		
		return results;
	}
	
	public List<Menu> obtenerMenuSalon(Integer idSalon){
		session=sessionFactory.openSession();

		Criteria cr = session.createCriteria(Menu.class)
				.add(Restrictions.eq("salon.idSalon", idSalon));
		List<Menu> results = cr.list();
		
		session.close();
		
		return results;
	}

	public List<Menu> obtenerMenu(Tipomenu tm, Clasificacionmenu cm){
		session=sessionFactory.openSession();
		
		Criteria cr = session.createCriteria(Menu.class);
		cr.add(Restrictions.eq("tipomenu.idTipo", tm.getIdTipo()));
		cr.add(Restrictions.eq("clasificacionmenu.idClasificacion", cm.getIdClasificacion()));
		List<Menu> results = cr.list();
		
		session.close();
		
		return results;
	}
	
	public void guardarMenu(Menu m, List<Detallemenu> listDetalle) throws Exception {
		
		DetallemenuId detallemenuid = new DetallemenuId();
		
		Transaction transaction = null;

		try{
			
		session = sessionFactory.openSession();
        transaction=session.beginTransaction();			
        // Registra Menu
        session.save(m);
        
        // Recupera el last_id de Menu
        Long lastId = ((BigInteger) session.createSQLQuery(
				"SELECT LAST_INSERT_ID()").uniqueResult()).longValue();

		int anInt = new BigDecimal(lastId).intValueExact();
		
		//Registra la lista de detalles de menu
        for(Detallemenu i: listDetalle){
        	detallemenuid.setIdMenu(anInt);
        	detallemenuid.setIdInsumo(i.getInsumo().getIdInsumo());
            i.setId(detallemenuid);
            session.save(i);
            detallemenuid = new DetallemenuId();
        }
		
		transaction.commit();
	
		                
        } catch (Exception e1) {
            //Error
            if (transaction != null) {
                transaction.rollback();
                throw e1;
              }
        } finally { 
            session.close();
        }
		
	
		}

	public List<Menu> obtenerMenuSalon(Tipomenu tipoMenuSelected,
			Clasificacionmenu clasificacionSelected, Integer idSalon) {
		session=sessionFactory.openSession();

		Criteria cr = session.createCriteria(Menu.class)
				.add(Restrictions.eq("tipomenu.idTipo", tipoMenuSelected.getIdTipo()))
		.add(Restrictions.eq("clasificacionmenu.idClasificacion", clasificacionSelected.getIdClasificacion()))
		.add(Restrictions.eq("salon.idSalon", idSalon));
		List<Menu> results = cr.list();
		
		session.close();
		
		return results;
	}

}
